package com.ht.right.service.impl;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ht.common.entity.PageInfo;
import com.ht.right.dao.CommonDao;
import com.ht.right.service.CommonService;
import com.ht.right.util.CommonUtil;

public class CommonServiceImpl<T> implements CommonService<T> {
	// -----通用数据列表加载开始-----
	private CommonDao<T> commonDao;


	@Override
	public List<T> loadAllList(String entityClsName) {
		List<T> listObj = commonDao.loadAllList(entityClsName);
		return listObj;
	}
	@Override
	public int getAllDataCount(String entityClsName,String docIds){
		return commonDao.getAllDataCount(entityClsName,docIds);
	}
	
	@Override
	public List<T> getPageDataList(String entityClsName,PageInfo pageInfo, String docIds){
		return commonDao.getPageDataList(entityClsName, pageInfo,docIds);
	}
	
	@Override
	public List<Map<String, String>> dataList(List<T> listObj, Set<String> fieldSet) {
		List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
		Map<String, String> map = null;
		Method m = null;
		String value = "";
		for (Object obj : listObj) {
			map = new HashMap<String, String>();
		/*	Field[] flied =  obj.getClass().getDeclaredFields();//反射出所有属性的值
			for (int i = 0; i < flied.length; i++) {
				Field attr = flied[i];
				attr.setAccessible(true);//设置这些属性都是可访问的
				try {
					String type = attr.getType().toString();
					//如果类型为日期和时间类型则进行转换
					if (type.contains("Date")) {
						value = CommonUtil.DateConversionString((Date)attr.get(obj));
					} else if (type.contains("Time")) {
						value = CommonUtil.DateConversion14String((Date)attr.get(obj));
					}else{
						value = (String) attr.get(obj);//属性值;
					}
					map.put(attr.getName(), value);
				} catch (Exception e) {
					e.printStackTrace();
				} 
			}*/
			
			for (String field : fieldSet) {
				// 拿到该属性的gettet方法
				try {
					m = (Method) obj.getClass().getMethod("get" + CommonUtil.upperFirstChar(field));
					//如果类型为日期和时间类型则进行转换
					if (m.getName().contains("Date")) {
						value = CommonUtil.DateConversionString((Date)m.invoke(obj));
					} else if (m.getName().contains("Time")) {
						value = CommonUtil.DateConversion14String((Date)m.invoke(obj));
					}else{
						value = (String) m.invoke(obj);
					}
				} catch (Exception e) {
					value = "";
					e.printStackTrace();
				} finally {
					if (value == null) {
						value = "";
					}
					map.put(field, value);
				}
			}
			dataList.add(map);
		}
		return dataList;
	}

	
	
	// -----通用数据列表加载结束-----


	public CommonDao<T> getCommonDao() {
		return commonDao;
	}


	public void setCommonDao(CommonDao<T> commonDao) {
		this.commonDao = commonDao;
	}
	@Override
	public Serializable save(T o) {
		return	commonDao.save(o);
	}
	@Override
	public void delete(T o) {
		// TODO Auto-generated method stub
		commonDao.delete(o);
	}
	@Override
	public void update(T o) {
		// TODO Auto-generated method stub
		commonDao.update(o);
	}
	@Override
	public void saveOrUpdate(T o) {
		// TODO Auto-generated method stub
		commonDao.saveOrUpdate(o);
	}
	@Override
	public T get(Class<T> c, Serializable id) {
		// TODO Auto-generated method stub
		return commonDao.get(c,id);
	}
	@Override
	public T getEntityByID(String entityClsName, String uuid) {
		// TODO Auto-generated method stub
		return commonDao.getEntityByID(entityClsName, uuid);
	}
	@Override
	public void deleteId(String model,String attr,String param) {
		String hql="delete from "+model+" where "+attr+" ='"+param+"'";
		commonDao.delete(hql);
	}
}
